import { http } from '@/utils'
import { SET_USER_INFO, USER_LOGOUT } from '@/store/actionTypes/user'
import { removeToken } from '@/utils/token'

const setUserInfo = userInfo => {
  return {
    type: SET_USER_INFO,
    payload: userInfo
  }
}

// 获取用户信息
export const getUserInfo = () => {
  return async dispatch => {
    const { data, message } = await http.get('/user/profile')

    if (message === 'OK') {
      dispatch(setUserInfo(data))
    }
  }
}

// 退出
export const logout = () => {
  return dispatch => {
    // 移除token
    removeToken()

    dispatch({
      type: USER_LOGOUT,
      payload: {}
    })
  }
}
